<template>
  <el-dialog
    :title="userId ? '修改答辩安排' :  '新增答辩安排'"
    :close-on-press-escape="false" 
    :close-on-click-modal="false"
    :append-to-body="true"
    :lock-scroll="true"
    :visible.sync="visible">
    <el-form
    label-position="right"  label-width="150px"
    status-icon
    :inline="true"
    size="small"
    :model="dataForm" :rules="dataRule" ref="dataForm"  @keydown.enter.native.prevent="dataFormSubmit()"  >
    <el-form-item label="指导教师" required prop="teaName">
        <el-select filterable v-model="dataForm.teaName" placeholder="指导教师" >
          <el-option v-for="item in teacherList" :lable="item" :value="item" :key="item.id" ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="答辩老师1" required prop="teaname1">
        <el-select filterable v-model="dataForm.teaname1" placeholder="答辩老师1" >
          <el-option v-for="item in teacherList" :lable="item" :value="item" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="答辩老师2" required prop="teaname2">
        <el-select filterable v-model="dataForm.teaname2" placeholder="答辩老师2" >
          <el-option v-for="item in teacherList" :lable="item" :value="item" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="答辩老师3" required prop="teaname3">
        <el-select filterable v-model="dataForm.teaname3" placeholder="答辩老师3" >
          <el-option v-for="item in teacherList" :lable="item" :value="item" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="答辩老师4" required prop="teaname4">
        <el-select filterable v-model="dataForm.teaname4" placeholder="答辩老师4" >
          <el-option v-for="item in teacherList" :lable="item" :value="item" :key="item.id"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="答辩时间" prop="starttime"  >
        <el-date-picker value-format="yyyy-MM-dd HH:mm:ss"
      v-model="dataForm.starttime"
      type="datetime"
      placeholder="选择日期时间">
    </el-date-picker>
      </el-form-item>
      <el-form-item label="答辩教室" required prop="place">
        <el-select filterable v-model="dataForm.place" placeholder="请输入答辩教室" >
          <el-option v-for="item in getDictList('class')" :label="item.dictLabel"  :key="item.id" :value="item.dictLabel"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import servicesApi from '@/services/servicesApi';
  import debounce from 'lodash/debounce';
  import { getDictDataList } from '@/utils';
export default {
  data() {
    // let quYu = (rule, value, callback) => { // 数字校验
    //   if (value == this.dataForm.teaName) {
    //     return callback(new Error('指导老师不能作为答辩老师'));
    //   }
    // };
    let tea1 = (rule, value, callback) => { // 数字校验
      if (value == this.dataForm.teaName) {
        return callback(new Error('指导老师不能作为答辩老师'));
      }
      else if (value == this.dataForm.teaname2) {
        return callback(new Error('答辩老师重复'));
      }
      else if (value == this.dataForm.teaname3) {
        return callback(new Error('答辩老师重复'));
      }
      else if (value == this.dataForm.teaname4) {
        return callback(new Error('答辩老师重复'));
      }
    };
    let tea2 = (rule, value, callback) => { // 数字校验
      if (value == this.dataForm.teaName) {
        return callback(new Error('指导老师不能作为答辩老师'));
      }
      else if (value == this.dataForm.teaname1) {
        return callback(new Error('答辩老师重复'));
      }
      else if (value == this.dataForm.teaname3) {
        return callback(new Error('答辩老师重复'));
      }
      else if (value == this.dataForm.teaname4) {
        return callback(new Error('答辩老师重复'));
      }
    };
    let tea3 = (rule, value, callback) => { // 数字校验
      if (value == this.dataForm.teaName) {
        return callback(new Error('指导老师不能作为答辩老师'));
      }
      else if (value == this.dataForm.teaname1) {
        return callback(new Error('答辩老师重复'));
      }
      else if (value == this.dataForm.teaname2) {
        return callback(new Error('答辩老师重复'));
      }
      else if (value == this.dataForm.teaname4) {
        return callback(new Error('答辩老师重复'));
      }
    };
    let tea4 = (rule, value, callback) => { // 数字校验
      if (value == this.dataForm.teaName) {
        return callback(new Error('指导老师不能作为答辩老师'));
      }
      else if (value == this.dataForm.teaname1) {
        return callback(new Error('答辩老师重复'));
      }
      else if (value == this.dataForm.teaname2) {
        return callback(new Error('答辩老师重复'));
      }
      else if (value == this.dataForm.teaname3) {
        return callback(new Error('答辩老师重复'));
      }
    };
    return {
      teacherList: [],
      repeatList: [],
      visible: false,
      userId: 0,
      dataForm: {
        teaName: 'a',
        teaname1: 'b',
        teaname2: 'c',
        teaname3: 'd',
        teaname4: 'e',
        starttime: null,
        place: '',
        tusername: '',
        tusername1: '',
        tusername2: '',
        tusername3: '',
        tusername4: '',
      },
      s: true,
      s1: true,
      s2: true,
      s3: true,
      s4: true,
      deleteList: {
              list: []
           },
      dataRule: {
        starttime: [
          { required: true, message: '答辩时间不能为空', trigger: 'change' },
        ],
        place: [
          { required: true, message: '答辩教室不能为空', trigger: 'change' }
        ],
        teaName: [
          { required: true, message: '指导教师不能为空', trigger: 'change' }
        ],
        teaname1: [
          { required: true, message: '答辩老师不能为空', trigger: 'change' },
          { validator: tea1, trigger: 'blur' },
        ],
        teaname2: [
          { required: true, message: '答辩老师不能为空', trigger: 'change' },
          {validator: tea2, trigger: 'blur' },
        ],
        teaname3: [
          { required: true, message: '答辩老师不能为空', trigger: 'change' },
          {validator: tea3, trigger: 'blur' },
        ],
        teaname4: [
          { required: true, message: '答辩老师不能为空', trigger: 'change' },
          {validator: tea4, trigger: 'blur' },
        ],
      }
    }
  },
  created() {
    this.getAllTea()
  },  
  methods: {

    //获取字典
    getDictList(dictType) {
      let list = getDictDataList(dictType)
      // // console.log(list)
      return list
    },
    //获取老师
    getAllTea() {
      servicesApi.getTeaList().then(res => {
        let code = res.data.code
        if (code == 200) {
          this.teacherList = res.data.data
          // this.$message({
          //   message: '操作成功',
          //   type: 'success',
          //   duration: 1500,
          // })
          // console.log(this.teacherList)
        } else {
          this.$message.error(res.data.msg)
        }
      })
    },
    init(id) {
      this.visible = true
        this.userId = id || 0
        if (this.userId) { //传入userID->修改
          // console.log('修改课题')
          servicesApi.getTarrange(this.userId).then(res => {
            let code = res.data.code
            let data = res.data.data
            if (code === 200) {
              // console.log(data)
              // this.dataForm = { state: 1, ...data }
              this.dataForm = data
            }
          })
        } else { //新增
          this.dataForm = {}
          // console.log('新增课题')
        }
    },
    // dataFormSubmit() {
        // this.$message({
        //   message: '操作成功',
        //   type: 'success',
        //   duration: 1500,
        //   onClose: () => {
        //     this.visible = false
        //     this.$emit('refreshDataList')
        //   }
        // })
        dataFormSubmit: debounce(function() {
        if (this.userId) {//修改
          let pageInfo = {
            id: this.userId,
            place: this.dataForm.place,
            starttime: this.dataForm.starttime,
            teaName: this.dataForm.teaName,
            teaname1: this.dataForm.teaname1,
            teaname2: this.dataForm.teaname2,
            teaname3: this.dataForm.teaname3,
            teaname4: this.dataForm.teaname4,
      }
      this.$refs['dataForm'].clearValidate() // 移除表单项的校验结果
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) { return false }
          servicesApi.submitCTarrange(pageInfo).then(res => {
            let code = res.data.code
            if (code == 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(res.data.msg)
            }
          })
        }) 
        } else { //新增
          this.$refs['dataForm'].clearValidate() // 移除表单项的校验结果
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              let pageInfo = {
            place: this.dataForm.place,
            starttime: this.dataForm.starttime,
            teaName: this.dataForm.teaName,
            teaname1: this.dataForm.teaname1,
            teaname2: this.dataForm.teaname2,
            teaname3: this.dataForm.teaname3,
            teaname4: this.dataForm.teaname4,
      }
              servicesApi.submitTarrange(pageInfo).then(res => {
                if (res.data.code === 200) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList') //回显
                    }
                  })
                } else {
                  this.$message.error(res.data.msg)
                }
              })
            }
          })
        }
  
      }, 1000, { 'leading': true, 'trailing': false })
}
  }
</script>
